package com.java.Servlet;

import com.java.entity.Users;
import com.java.service.UserService;
import com.java.service.Impl.UserServiceImpl;
import com.java.util.MyException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    private UserService service =new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String mark = req.getParameter("mark");
        if (mark.equals("userLogin")){
            doUserLogin(req,resp);
        }else if (mark.equals("userRegister")){
            doUserRegister(req,resp);
        }else if (mark.equals("updateUser")){
            doUpdateUser(req,resp);
        }else if (mark.equals("outUser")){
            doOutUser(req,resp);
        }
    }

    /*
    用户退出
     */
    protected void doOutUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //移除登录用户的session
        req.getSession().removeAttribute("user");
        //跳转到登录页面
        req.getRequestDispatcher("/user/test02/login.jsp").forward(req,resp);
    }
    /*
    修改用户信息
     */
    protected void doUpdateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userid").trim();
        String pwd = req.getParameter("pwd").trim();
        String IDCar = req.getParameter("IDCar").trim();
        String phone = req.getParameter("phone").trim();
        String name = req.getParameter("name").trim();
        String sex = req.getParameter("sex").trim();
        String age = req.getParameter("age").trim();
        String addr = req.getParameter("addr").trim();

        Users user=new Users(name,sex,Integer.parseInt(age),IDCar,phone,addr,userId,pwd);
        //调用service层代码
        try {
            service.updateUser(user);
            //修改session中的用户数据
            req.getSession().setAttribute("user",user);
            //退出登录并跳转回登录页面
            doOutUser(req,resp);
            //req.getRequestDispatcher("/user/test02/userInfo.jsp").forward(req,resp);
        } catch (MyException e) {
            e.printStackTrace();
            req.setAttribute("eorr",e.getMessage());
            req.getRequestDispatcher("/user/test02/eorr.jsp").forward(req,resp);
        }
    }
    /*
    用户注册
     */
    protected void doUserRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId").trim();
        String pwd = req.getParameter("pwd").trim();
        String IDCar = req.getParameter("IDCar").trim();
        String phone = req.getParameter("phone").trim();
        String name = req.getParameter("name").trim();
        String sex = req.getParameter("sex").trim();
        String age = req.getParameter("age").trim();
        String addr = req.getParameter("addr").trim();

        Users user=new Users(name,sex,Integer.parseInt(age),IDCar,phone,addr,userId,pwd);
        //调用service层代码
        try {
            service.register(user);
            req.getRequestDispatcher("/user/test02/login.jsp").forward(req,resp);
        } catch (MyException e) {
            e.printStackTrace();
            req.setAttribute("eorr",e.getMessage());
            req.getRequestDispatcher("/user/test02/register.jsp").forward(req,resp);
        }
    }
    /*
    用户根据userId、pwd登录
     */
    protected void doUserLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端表单的数据用户名和密码
        String userId = req.getParameter("userId").trim();
        String pwd = req.getParameter("pwd").trim();
        try {
            Users user = service.login(userId, pwd);
            if(user==null){
                req.setAttribute("loginMsg","登录失败！用户名或密码错误");
                req.getRequestDispatcher("/user/test02/login.jsp").forward(req,resp);
                return;
            }
            //将登录的用户的信息存入session
            req.getSession().setAttribute("user",user);
            //登录成功后跳转到首页
            req.getRequestDispatcher("/user/test03/normal/index.jsp").forward(req,resp);
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
        @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
